package org.itsolutions.javaBackup.gui.web.common;

import java.util.Collections;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;

import org.itsolutions.javaBackup.gui.web.common.resources.IResourceBundle;
import org.itsolutions.javaBackup.gui.web.common.resources.ResourceBundle;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;

import com.google.inject.Inject;
import com.vaadin.Application;

/**
 * 
 * @author Tomas Fecko
 */
public final class ApplicationDataHolder {

	private static final ILogger					LOGGER = LoggerHelper.getLogger(ApplicationDataHolder.class);
	private static final IResourceBundle			DUMMY_BUNDLE = new DummyResourceBundle();
    private EGUIApplication 						mApplication;
    private Locale 									mLocale;
    private Map<EGUIApplication, IResourceBundle>	mBundles;
    private Application								mVaadinApplication;
    
    /**
     * 
     */
	@Inject ApplicationDataHolder() {
		mBundles = Collections.synchronizedMap(new EnumMap<EGUIApplication, IResourceBundle>(EGUIApplication.class));
	}

	/**
     * 
     * @return locale
     */
    public Locale getLocale() {
        return mLocale;
    }

    /**
     * 
     * @param pApplication application
	 * @param pLocale the locale to set
	 * @return this
	 */
	public ApplicationDataHolder setApplicationAndLocale(EGUIApplication pApplication, Locale pLocale) {
		mLocale = pLocale;
		mApplication = pApplication;
		return this;
	}

	/**
	 * @param pApplication the application to set
	 */
	public void setApplicationEnum(EGUIApplication pApplication) {
		mApplication = pApplication;
	}

	/**
     * 
     * @return eapp
     */
    public EGUIApplication getApplicationEnum() {
        return mApplication;
    }

	/**
	 * @return the vaadinApplication
	 */
	public Application getApplication() {
		return mVaadinApplication;
	}
	
	/**
	 * 
	 */
	public void logout() {
		if (mVaadinApplication instanceof IAutenticatedApplication) {
			((IAutenticatedApplication) mVaadinApplication).logout();
		} else {
			mVaadinApplication.close();
		}
	}

	/**
	 * @param pVaadinApplication the vaadinApplication to set
	 */
	public void setApplication(Application pVaadinApplication) {
		mVaadinApplication = pVaadinApplication;
	}

	/**
	 * @return the bundle
	 */
	private IResourceBundle getBundle() {
		if (mApplication == null) {
			LOGGER.log(ELogLevel.ERROR, "GUI Application is not set in ApplicationDataHolder.");
			return DUMMY_BUNDLE;
		}
		IResourceBundle resourceBundle = mBundles.get(mApplication);
		if (resourceBundle == null) {
			resourceBundle = ResourceBundle.getBundle(mApplication, mLocale);
			mBundles.put(mApplication, resourceBundle);
		}
		return resourceBundle;
	}
	
	/**
	 * Get localized string under that key.
	 * @param pKey property key
	 * @return localized string
	 */
	public String getString(String pKey) {
		return getBundle().getString(pKey);
	}
	
	/**
	 * 
	 */
	private static final class DummyResourceBundle implements IResourceBundle {
		@Override
		public String getString(String pKey) {
			return "??";
		}
	}
}





